Destructuring and spread syntax consume iterables by calling Symbol.iterator and pulling values until done, allowing them to work with any custom iterable.
When you write [a, b, ...rest] = iterable, JavaScript calls iterable[Symbol.iterator]() and reads values until the pattern is satisfied. Similarly, [...iterable] collects all values. This is why you can use spread on Set, Map (with caution), String, and custom iterables.
You need to copy an array of numbers and add one more element at the end using spread syntax. How would you write that, and what does JavaScript do under the hood with the iterator protocol?
When you destructure an object that has a Symbol.iterator property, what values do you get in the variables, and why?
If you try to spread a plain object into an array, what error do you see and why?
We have a function that receives a mixed array of strings and numbers, and we want to separate the first two items via destructuring and collect the rest using the rest operator. The code breaks when the input is a Set. Explain why and how you would fix it.
A teammate spread a custom class instance that implements Symbol.iterator but also has own enumerable properties, and the resulting array includes unexpected values. Walk me through why that happens and how to control what gets spread.
Why does spreading a Map into an object using {...myMap} produce an empty object, and how does the iterator protocol factor in?
We need a utility that deep clones objects and also correctly clones iterable structures like Sets and Maps using spread. Discuss the design considerations, performance implications, and how you would ensure the iterator protocol is respected.
Our custom lazy sequence class implements Symbol.iterator. When we spread its instances into an array, it eagerly evaluates the whole sequence, causing memory pressure. How would you redesign the spread usage or the class to mitigate this?
Explain the trade‑offs of using destructuring with default values on large data streams that are iterables, considering the iterator protocol's one‑pass nature.
We are migrating a legacy codebase that heavily uses manual for‑of loops over custom iterables to modern destructuring and spread syntax. What architectural guidelines would you set to ensure correctness, performance, and maintainability, especially regarding iterator side effects?
Across multiple teams, some spread objects assuming they are iterable while others rely on custom toArray methods. Propose a cross‑team policy or abstraction that standardizes how iterables are consumed, considering the iterator protocol and future JavaScript proposals.
How would you evaluate the impact of adopting a new language feature like async destructuring on existing code that relies on the iterator protocol for spread and destructuring, and plan a migration strategy?